<?php

namespace App\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

class LazyMoveToCommonCommand extends Command
{
    protected static $defaultName = 'lazy:move-to-common';

    protected function configure()
    {
        $this
            ->setDescription('move a normal cell to LazyCommon')
            ->addArgument(
                'name',
                InputArgument::OPTIONAL,
                'the name of a cell ,eg:AdminLog will move AdminLogModel and AdminLogSetting and so on'
            )
//            ->addOption('option1', null, InputOption::VALUE_NONE, 'Option description')
        ;
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $io = new SymfonyStyle($input, $output);
        $name = $input->getArgument('name');

        if ($name) {
            $io->note(sprintf('get name : %s', $name));
        }

        $root = str_replace("src\\Command","",__DIR__);//E:\www\symfony4\src\Command
        //$root = E:\www\symfony4\
        $io->note(sprintf('get name : %s', $name));

        //准备移动controller
        $controlFile = $root."src\\Controller\\Admin\\{$name}"."Controller.php";
        $controlFileNew = $root."src\\Controller\\LazyCommon\\{$name}"."Controller.php";
        if (file_exists($controlFile)) {
            $io->note("找到文件：".$controlFile);
            $controlFileContent = file_get_contents($controlFile);
            $controlFileContent = str_replace("namespace App\Controller\Admin;","namespace App\Controller\LazyCommon;",$controlFileContent);
            $controlFileContent = str_replace("App\\Setting\\".$name."Setting;","App\\Setting\\LazyCommon\\".$name."Setting;",$controlFileContent);
            $controlFileContent = str_replace("App\\Model\\$name"."Model;","use App\Model\LazyCommon\\$name"."Model;",$controlFileContent);
            file_put_contents($controlFileNew,$controlFileContent);
            $io->note("正在移动");
            copy($controlFile,$controlFile.".bkp.".date("YmdHis"));
            unlink($controlFile);
        }else{
            $io->note("controller文件不存在：".$controlFile);
        }
        //准备移动Model
        $ModelFile = $root."src\\Model\\{$name}"."Model.php";
        $ModelFileNew = $root."src\\Model\\LazyCommon\\{$name}"."Model.php";
        if (file_exists($ModelFile)) {
            $io->note("找到文件：".$ModelFile);
            $ModelFileContent = file_get_contents($ModelFile);
            $ModelFileContent = str_replace("namespace App\Model;","namespace App\Model\LazyCommon;",$ModelFileContent);
            $ModelFileContent = str_replace("App\\Setting\\".$name."Setting;","App\\Setting\\LazyCommon\\".$name."Setting;",$ModelFileContent);
            $ModelFileContent = str_replace("App\\Model\\$name"."Model;","use App\Model\LazyCommon\\$name"."Model;",$ModelFileContent);
            file_put_contents($ModelFileNew,$ModelFileContent);
            $io->note("正在移动");
            copy($ModelFile,$ModelFile.".bkp.".date("YmdHis"));
            unlink($ModelFile);
        }else{
            $io->note("Model文件不存在：".$ModelFile);
        }
        //准备移动Setting
        $SettingFile = $root."src\\Setting\\{$name}"."Setting.php";
        $SettingFileNew = $root."src\\Setting\\LazyCommon\\{$name}"."Setting.php";
        if (file_exists($SettingFile)) {
            $io->note("找到文件：".$SettingFile);
            $SettingFileContent = file_get_contents($SettingFile);
            $SettingFileContent = str_replace("namespace App\Setting;","namespace App\Setting\LazyCommon;",$SettingFileContent);
            $SettingFileContent = str_replace("App\\Setting\\".$name."Setting;","App\\Setting\\LazyCommon\\".$name."Setting;",$SettingFileContent);
            $SettingFileContent = str_replace("App\\Setting\\$name"."Setting;","use App\Setting\LazyCommon\\$name"."Setting;",$SettingFileContent);
            file_put_contents($SettingFileNew,$SettingFileContent);
            $io->note("正在移动");
            copy($SettingFile,$SettingFile.".bkp.".date("YmdHis"));
            unlink($SettingFile);
        }else{
            $io->note("Setting文件不存在：".$SettingFile);
        }

        $io->success('You have a new command! Now make it your own! Pass --help to see your options.');
    }
}
